from rest_framework import serializers
from .models import *
import re


class UserSer(serializers.ModelSerializer):

    # 自定义一个 验证方法， 来验证 手机号字段
    def validate_phone(self, phone):
        if not re.match("^1[3-9]\d{9}$", phone):
            # 不是手机号 则 抛出异常
            raise serializers.ValidationError(detail="手机号格式错误", code=400)
        return phone

    class Meta:
        model = UserProfile
        fields = "__all__"

    def create(self, validated_data):#{'username':'123','age':20}
        return UserProfile.objects.create_user(**validated_data)#  username=123,age=20
